import request from '@/utils/request.js'

// 获取教师列表
export function getTeacherList(params) {
  return request({
    url: 'teachers',
    method: 'GET',
    params
  })
}
// 实名认证
export function submitRealNameAuth(data) {
    return request({
        url: 'teacher/auth',
        method: 'POST',
        data
    })
}

// 搜索教师
export function searchTeachers(params){
	 // 确保科目参数正确传递
	const apiParams = { ...params };
	    
	// 清空查询参数中的空字符串
	Object.keys(apiParams).forEach(key => {
	    if (apiParams[key] === '') {
	    delete apiParams[key];
	     }
	});
	    
	console.log('搜索教师参数:', apiParams);
	    
	return request({
	    url: `teachers/search`,
	    method: 'GET',
	    params: apiParams,
		
	})
}


// 获取教师详情
export function getTeacherDetail(id) {
  return request({
    url: `teachers/detail`,
    method: 'GET',
	params: {id}
  })
}

// 教师认证申请 暂时弃用
export function applyTeacherCertification(data) {
  return request({
    url: 'teachers/certification',
    method: 'POST',
    data
  })
}

// 教师注册
export function submitRegistration(data){
	return request({
		url: 'teacher/register',
		method: 'POST',
		data
	})
}


// 获取认证状态
export function getCertificationStatus(userid) {
  return request({
    url: 'teacher/certification/status',
    method: 'GET',
      params: {"userId":userid}

  })
}

// 收藏教师
export function favoriteTeacher(teacherId) {
  return request({
    url: 'teachers/favorite',
    method: 'POST',
    data: { teacherId }
  })
}

// 取消收藏教师
export function unfavoriteTeacher(teacherId) {
  return request({
    url: `teachers/favorite/${teacherId}`,
    method: 'DELETE'
  })
}

// 获取教师学生列表
export function getTeachersStudent(userId) {
  return request({
    url: 'teacher/students',
    method: 'GET',
    params: { userId }
  })
}

// 完成授课
export function completeCourse(data) {
  return request({
    url: 'teacher/course/complete',
    method: 'POST',
    data
  })
}

// 获取教师排行榜
export function getTeacherRanking(params) {
  return request({
    url: 'teacher/ranking',
    method: 'GET',
    params
  })
}

// 添加教师可授课时间
export function addAvailableTime(data) {
  return request({
    url: 'teacher/available-time/add',
    method: 'POST',
    data
  })
}

// 获取教师可授课时间列表
export function getAvailableTimes(params) {
  return request({
    url: 'teacher/available-time/list',
    method: 'GET',
    params
  })
}

// 删除教师可授课时间
export function removeAvailableTime(data) {
  return request({
    url: 'teacher/available-time/remove',
    method: 'POST',
    data
  })
}

// 批量添加教师可授课时间
export function batchAddAvailableTime(data) {
  return request({
    url: 'teacher/available-time/batch-add',
    method: 'POST',
    data
  })
}

// 申请成为长期教师
export function applyForLongTerm(data) {
  return request({
    url: 'teacher/apply-long-term',
    method: 'POST',
    data
  })
}

// 获取长期教师申请状态
export function getLongTermApplicationStatus(params) {
  return request({
    url: 'teacher/long-term-application-status',
    method: 'GET',
    params
  })
}

// ========== 课程排课相关API ==========

// 创建新课程并分配给教师
export function createAndAssignCourse(data) {
  return request({
    url: 'teacher/course/create-and-assign',
    method: 'POST',
    data
  })
}

// 将现有课程分配给教师
export function assignExistingCourse(data) {
  return request({
    url: 'teacher/course/assign-existing',
    method: 'POST',
    data
  })
}

// 批量分配课程给教师
export function batchAssignCourses(data) {
  return request({
    url: 'teacher/course/batch-assign',
    method: 'POST',
    data
  })
}

// 查询教师已分配的课程
export function getAssignedCourses(params) {
  return request({
    url: 'teacher/course/assigned-list',
    method: 'GET',
    params
  })
}

// 查询教师可分配的课程列表
export function getAvailableCourses(params) {
  return request({
    url: 'teacher/course/available-list',
    method: 'GET',
    params
  })
}

// 取消教师课程分配
export function unassignCourse(data) {
  return request({
    url: 'teacher/course/unassign',
    method: 'POST',
    data
  })
}

// 查询课程的授课教师列表
export function getCourseTeachers(params) {
  return request({
    url: 'teacher/course/teachers',
    method: 'GET',
    params
  })
}

// 导出teacherApi对象
export const teacherApi = {
  getTeacherList,
  getTeacherDetail,
  applyTeacherCertification,
  getCertificationStatus,
  favoriteTeacher,
  unfavoriteTeacher,
  searchTeachers,
  submitRegistration,
  getTeachersStudent,
  completeCourse,
  getTeacherRanking,
  submitRealNameAuth,
  addAvailableTime,
  getAvailableTimes,
  removeAvailableTime,
  batchAddAvailableTime,
  applyForLongTerm,
  getLongTermApplicationStatus,
  // 课程排课相关
  createAndAssignCourse,
  assignExistingCourse,
  batchAssignCourses,
  getAssignedCourses,
  getAvailableCourses,
  unassignCourse,
  getCourseTeachers
}